﻿using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace InterfaceExample
{
    //委托
    #region

    class Delegate
    {
        delegate void MyDele();
        delegate int Cal(int a,int b);
        static void Main1(string[] args)
        {
            //委托包裹方法，而不是直接调用
            MyDele myDele = new MyDele(Student1);
            myDele();

            Student student = new Student();
            MyDele myDele1 = new MyDele(student.SayHello);
            myDele1 += student.SayHello;//调用两次，多播委托
            myDele1 += new Vehicle().Run;
            myDele1();

            Cal calSum = new Cal(Sum);
            int res = calSum(100,200);
            Console.WriteLine(res);
        }
        static void Student1()
        {
            Console.WriteLine("I'm Foster");
        }
        static int Sum(int a,int b)
        {
            //Console.WriteLine(a+b);
            return a+ b;    
        }
    }
    class Student
    {
        public void SayHello()
        {
            Console.WriteLine("Hello,I'Genser");
        }
    }
    class Vehicle
    {
        public void Run()
        {
            Console.WriteLine("Vehicle running...");
        }
    }
    #endregion

    //泛型委托
    #region
    class TDelegate
    {
        //delegate T Cal<T>(T a, T b);//泛型委托声明
        static void Main2(string[] args)
        {
            /*Cal<int> calSum = new Cal<int>(Sum);
            int result = calSum(100,200);
            Console.WriteLine(result);

            Cal<double> calMul = new Cal<double>(Mul);
            double mResult = calMul(100.11,200.22);
            Console.WriteLine(mResult);
            Console.WriteLine(calMul.GetType().IsClass);//委托是一种类*/
            Action<string,int> action = new Action<string,int>(Say);
            action("LiHua",3);
            var funSum = new Func<int,int,int>(Sum);
            var res = funSum(1, 3);
            Console.WriteLine(res);

            var funMul = new Func<double, double, double>(Mul);
            var res1 = funMul(1.1, 9.9);
            Console.WriteLine(res1);
        }
        static void Say(string name,int round)
        {
            for (int i = 0; i < round; i++) Console.WriteLine($"Hello,My Name is{name}");
        }
        static int Sum(int a,int b)
        {
            return a+ b;
        }
        static double Mul(double a, double b)
        {
            return a*b;
        }
    }
    #endregion

    //Lambda,语法糖，类型推断
    #region
    class Lambda
    {
        //表达式，1.匿名方法，2.inLine方法
        static void Main1(string[] args)
        {
            //lambda表达式
            Func<int, int, int> funcSum = new Func<int, int, int>((int a, int b) => { return a + b; });
            var resSum = funcSum(1, 3);
            Console.WriteLine(resSum);
            //简写
            funcSum = (a, b) => { return a * b; };
            var resMul = funcSum(1, 3); 
            Console.WriteLine(resMul);

            DoSomeCal<double>((a, b) => { return a * b; }, 3.3, 4.4);
        }
        //泛型方法，泛型委托，lambda表达式
        static void DoSomeCal<T>(Func<T, T, T> sumFunc,T a,T b)
        {
            var res = sumFunc(a,b);
            Console.WriteLine(res);
        }
    }
    #endregion

    //LINQ
    #region
    class LINQ
    {
        //.Net,Entity.Net,EntityFramwork,Asp.Net
        static void Main1(string[] args) 
        {
            //查询表达式
            int[] nums1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 ,10};
            var result = from x in nums1 where x%2 == 0 select x;
            string output = string.Join(",",result);
            Console.WriteLine(output);

            foreach (int x in result)
            {
                Console.WriteLine(x);
            }
            //方法语法
            var result1 = nums1.Where( x => x % 3 == 0);
            foreach (int x in result1)
            {
                Console.WriteLine(x);
            }
            string output1 = string.Join(",",result1);
            Console.WriteLine(output1);

        }
    }
    #endregion

    class EventArgs
    {
        //自定义委托
        public delegate void MyDelegate(string Message);

        //内置Action委托，无返回值
        static Action<int>? TemperatureChanged = CheckTemperature ;

        //内置Func委托，有返回值
        static Func<int, int, int>? CalAdd = Add;

        public event MyDelegate Click;

        static void Main(string[] args)
        {
            //委托
            MyDelegate myDelegate = new MyDelegate(Print);
            myDelegate("OK ");

            //多播委托
            MyDelegate myDelegate2 = new MyDelegate(PrintApple);
            myDelegate2 += new MyDelegate(PrintAnotherApple);
            myDelegate2("Apple");

            TemperatureChanged(60);
            CalAdd(10,20);

            
        }
        public void OnClick()
        {
            Console.WriteLine("Button Click");
        }
        
        private static int Add(int arg1, int arg2)
        {
            var result = arg1 + arg2;
            Console.WriteLine(result);
            return result;
        }
        public static void CheckTemperature(int temp)
        {
            if(temp>30)
                Console.WriteLine("温度超过30度"); ;
        }
        public static void Print(string message)
        {
            Console.WriteLine(message);
        }
        public static void PrintApple(string message) 
        { 
            Console.WriteLine(message); 
        }
        public static void PrintAnotherApple(string message) 
        { 
            Console.WriteLine("Another " + message); 
        }
    }
}
